﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Longbow.Caching;

namespace HuaCheng.DataAccess
{
    /// <summary>
    /// 历史故障的数据库操作
    /// </summary>
    public class ErrorHelper
    {
        internal const string RetrieveErrorLogDataKey = "ErrorHelper-RetrieveErrorLog";
        /// <summary>
        /// 插入故障到历史故障表
        /// </summary>
        /// <param name="errList"></param>
        public static void ModifyHistoryErr(List<Errors> errList)
        {
            foreach (Errors err in errList)
            {
                try
                {
                    string sqlStr = "insert into h_error (dev_ip,cabinet_id,mid_id,dev_chl_id,occur_time,err_cause) values(@devip,@gid,@mid,@chlid,@occurtime,@cause)";
                    using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr))
                    {
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", err.IP));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", err.GID + 1));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", err.MID + 1));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@chlid", err.CID + 1));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@occurtime", err.OccurTime));
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cause", err.ErrorCause));
                        DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                    }

                }
                catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
            }
        }
        /// <summary>
        /// 根据设备查询最新一条历史故障
        /// </summary>
        /// <param name="devip"></param>
        /// <returns></returns>
        public static Errors GetHistoryErrors(string devip, int gid, int mid,int cid)
        {
            Errors err = new Errors();
            try
            {
                string sqlStr = "select * from h_error where dev_ip=@ip and cabinet_id=@gid and mid_id=@mid and dev_chl_id=@cid order by occur_time desc limit 1";
                DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sqlStr);
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ip", devip));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cid));
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            err = new Errors()
                            {
                                IP = Convert.ToString(reader["dev_ip"]),
                                MID = Convert.ToInt32(reader["cabinet_id"]),
                                GID = Convert.ToInt32(reader["mid_id"]),
                                CID = Convert.ToInt32(reader["dev_chl_id"]),
                                ErrorCause = Convert.ToString(reader["err_cause"]),
                                OccurTime = Convert.ToString(reader["occur_time"])
                            };
                        }
                    }
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
            return err;
        }
        /// <summary>
        /// 查询log_mid表所有信息
        /// </summary>
        /// <returns></returns>
        public static List<Errors> RetrieveErrorLog(string devip, int gid,int mid, int cid)
        {
            string key = RetrieveErrorLogDataKey;
            return CacheManager.GetOrAdd(key, k =>
            {
                List<Errors> ret = new List<Errors>();
                try
                {
                    string sql = "select * from h_error where dev_ip=@devip and cabinet_id=@gid and mid_id=@mid and dev_chl_id=@cid";
                    DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@devip", devip));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@gid", gid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@mid", mid));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cid", cid));
                    using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Errors errorLog = new Errors()
                                {
                                    IP = Convert.ToString(reader["dev_ip"]),
                                    GID = Convert.ToInt32(reader["cabinet_id"]),
                                    MID = Convert.ToInt32(reader["mid_id"]),
                                    CID = Convert.ToInt32(reader["dev_chl_id"]),
                                    OccurTime = Convert.ToString(reader["occur_time"]),
                                    ErrorCause = Convert.ToString(reader["err_cause"]),
                                };
                                ret.Add(errorLog);
                            }
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
                return ret;
            }, RetrieveErrorLogDataKey);
        }
    }
}
